package com.example.chapter5.controller;

import com.example.chapter5.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

//@Controller
//@ResponseBody
@RestController
public class StudentController {
    //通过id查找学生数据
//    @RequestMapping(value = "/students/{id}",method = RequestMethod.GET)
    @Autowired
    StudentMapper studentMapper;

    // 根据id删除学生信息（DELETE请求）
    @DeleteMapping(value = "/student/{id}")
    public ResponseEntity<String> deleteStudent(@PathVariable("id") int id) {
        int result = studentMapper.deleteStudent(id);
        if (result > 0) {
            return ResponseEntity.ok("学生信息删除成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除学生信息失败");
        }
    }

    @GetMapping(value = "/student/{id}")
    public Object student(@PathVariable("id") int id) {
        return studentMapper.findById(id);
    }
}
